home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsIII / rgbvary.c < prev    next >
Text File  |  1992-06-16  |  3KB  |  96 lines

  1. /****************************************************************************
  2.     rgbvary.c - a color quantization pre-processor
  3.  
  4.     The function jitter_init() must be call one time before any calls
  5.     to rgbvary().
  6.  
  7. Entry:
  8.     rgb            - an array of RGB components
  9.     noise_level    - the amount of noise desired (0 - 8)
  10.     x              - the x coordinate of the pixel being processed
  11.     y              - the y coordinate of the pixel being processed
  12.  
  13. Exit:
  14.     rgb            - the resulting array of RGB components
  15. ****************************************************************************/
  16.  
  17. /* defined constants */
  18. #define LARGE_NUMBER        1024
  19. #define JITTER_TABLE_SIZE     1024
  20. #define JITTER_MASK         (JITTER_TABLE_SIZE-1)
  21.  
  22. /* global varables */
  23. int       irand[JITTER_TABLE_SIZE];    /* jitter look-up table */
  24. double    uranx[JITTER_TABLE_SIZE];    /* jitter look-up table */
  25. double    urany[JITTER_TABLE_SIZE];    /* jitter look-up table */
  26.  
  27. /* jitter macros */
  28. #define jitterx(x,y,s) (uranx[((x+(y<<2))+irand[(x+s)&JITTER_MASK])&JITTER_MASK])
  29. #define jittery(x,y,s) (uranx[((y+(x<<2))+irand[(y+s)&JITTER_MASK])&JITTER_MASK])
  30.  
  31. /* function declarations */
  32. void jitter_init();
  33. int rand();
  34.  
  35. void rgbvary(unsigned char rgb[3], int noise_level, int x , int y)
  36.     {
  37.     int i, p, q;
  38.  
  39.     if (noise_level == 0)
  40.         {
  41.         for (i = 0; i <= 2; ++i)
  42.             rgb[i] &= 0xf8;
  43.         return;
  44.         }
  45.  
  46.     for (i = 0; i <= 2; ++i)
  47.         {
  48.         if (rgb[i] < 248)
  49.             {
  50.             p = rgb[i] % 8;
  51.             q = (int)(jitterx(x, y, i) * 9.0);
  52.             if (p <= q)
  53.                 rgb[i] += 8;
  54.  
  55.             /* add some noise */
  56.             q = 8 * ((int)((jittery(x, y, i)
  57.                 * (double)(2 * noise_level)) + 0.5)
  58.                 - noise_level);
  59.             q += (int)rgb[i];
  60.  
  61.             /* make sure resulting intensity is within allowable range */
  62.             if (q >= 0 && q <= 255)
  63.                 rgb[i] = q;
  64.  
  65.             /* mask off lower 3 bits */
  66.             rgb[i] &= 0xf8;
  67.             }
  68.         }
  69.     }
  70.  
  71. /*
  72. *
  73. * name        jitter_init - initialize jitter look-up tables
  74. *
  75. *             Adapted from Graphic Gems I (Cyshosz, page 64).
  76. *
  77. * notes       This function should be called once before any call to
  78. *             rgbvary()
  79. *
  80. */
  81.  
  82. void jitter_init()
  83.     {
  84.     int i;
  85.  
  86.     /* initialize look-up tables */
  87.     for (i = 0; i < JITTER_TABLE_SIZE; ++i)
  88.         uranx[i] = (double)(rand() % LARGE_NUMBER) / (double)LARGE_NUMBER;
  89.     for (i = 0; i < JITTER_TABLE_SIZE; ++i)
  90.         urany[i] = (double)(rand() % LARGE_NUMBER) / (double)LARGE_NUMBER;
  91.     for (i = 0; i < JITTER_TABLE_SIZE; ++i)
  92.         irand[i] = (int)((double)JITTER_TABLE_SIZE
  93.             * ((double)(rand() % LARGE_NUMBER) / (double)LARGE_NUMBER));
  94.     }
  95.  
  96.